function inArray(elem, arr){
  for (var v of arr){
    if (v === elem){
      return true;
    }
  }
  return false;
}
/**
 * url:需要处理的url
 * encodeFlag:是否对url进行编码,默认为false
 * 
 * var url = UUrl(url)
 *		.addPara("name","abc")
 *		.addPara("age","12")
 * 		.get(true);
 */
var Url = function(input) {
	/**
	 * 初始化
	 */
  this.url = "";
  this.params = {};	//url参数对象
  var path = "";
	/**
	 * 增加参数  addPara(a,1).addPara(a,1),addPara(a,2) => [a=1,a=2]
	 */
  this.addPara = function (name, para) {
    if (name) {
      name = name.trim();
      var values = this.params[name];
      if (!values) {
        values = this.params[name] = [];
      }
      if (null != para) {
        para = String(para).trim();
        if (!inArray(para, values)) {
          values.push(para);
        }
      }
    }
    return this;
  };
	/**
	 * 处理path
	 *  1. 'http://www.baidu.com:80'	-> ''
	 *  2. 'http://www.baidu.com:80/'	-> '/'
	 *  3. 'http://www.baidu.com:80/a'	-> '/a'
	 */
  this.dealPath = function (url) {
    if (url) {
      url = url.trim();
      if (url.substring(0, 7) != 'http://' && url.substring(0, 8) != 'https://') {
        this.path = url;
      }
      var p = url.indexOf('/', url.indexOf('://') + 3);
      if (p == -1) {
        this.path = '';
      } else {
        this.path = url.substring(p)
      }
    }
  };
	/**
	 * 处理url中已有参数
	 */
  this.dealUrlAndParas = function (href, url) {
    var str = href;
    if (url) {
      url = url.trim();
      if (url.substring(0, 7) != 'http://' && url.substring(0, 8) != 'https://') {
        //if(url.indexOf("http://")==-1 && url.indexOf("https://")==-1){
        if (href.lastIndexOf('/') !== (href.length - 1) && url.charAt(0) != '/') {
          str += "/";
        }
        str += url;
      } else {
        str = url;
      }
    }
    var p = str.indexOf("?");
    if (p != -1) {
      var paraStr = str.substring(p + 1);
      if (paraStr) {
        var arrs = paraStr.split("&");
        for (var i = 0; i < arrs.length; i++) {
          var paraItem = arrs[i].trim();
          if (paraItem) {
            var t = paraItem.split("=");
            this.addPara(t[0], t[1] ? t[1] : "");
          }
        }
      }
      str = str.substring(0, p).trim();
    }
    if (str.charAt(str.length - 1) == '/') {
      str = str.substring(0, str.length - 1);
    }
    this.url = str;
  };
	/**
	 * init
	 */
  this.init = function (input) {
    var href = "";
    var url = "";
    if (input) {
      url = input.trim();
      var firstChar = url.charAt(0);
      if (firstChar == "/") {			//1. /a/b/c
      } else if (firstChar == "+") {		//2. +a/b/c 或 +/a/b/c
        url = url.substring(1);			// +a/b/c -> a/b/c , +/a/b/c -> /a/b/c
        if (url.charAt(0) == '/') {
          url = url.substring(1);		// /a/b/c -> a/b/c
        }
        href += path;
      } else {							//3 a/b/c
        if (firstChar == '?') {		// ?x=1&y=2
          href += path;
        } else {							// a/b/c?x=1&y=2
          var index = path.lastIndexOf('/');
          href += path.substring(0, index);
        }
      }
    }
    this.dealUrlAndParas(href, url);
    this.dealPath(this.url)
  };
  this.init(input);
	/**
	 * 设置参数
	 */
  //setPara
  this.setPara = function (name, para) {
    if (name) {
      name = name.trim();
      if (para instanceof Array) {
        this.params[name] = para;
      } else {
        this.params[name] = [para];
      }
    }
    return this;
  };
  //setParas
  this.setParas = function (paras) {
    for (var name in paras) {
      this.setPara(name, paras[name]);
    }
    return this;
  };
  //getParas
  this.getParas = function () {
    return this.params;
  };
  //getParasStr
  this.getParasStr = function () {
    var objps = [];
    for (var name in this.params) {
      var values = this.params[name];
      if (values) {
        for (var i = 0; i < values.length; i++) {
          objps.push(name + "=" + values[i]);
        }
      }
    }
    if (objps.length > 0) {
      return "?" + objps.join("&");
    } else {
      return "";
    }
  };
	/**
	 * 删除参数
	 */
  this.removePara = function (name) {
    if (name) {
      delete this.params[name.trim()];
    }
    return this;
  };
	/**
	 * 清除参数
	 */
  this.clearParas = function () {
    this.params = {};
    return this;
  };
	/**
	 * 更改url
	 */
  this.refreshUrl = function (url) {
    var p = url.indexOf("?");
    if (p != -1) {
      url = url.substring(0, p)
    }
    this.init(url);
    return this;
  };
	/**
	 * getInput
	 */
  this.getInput = function () {
    return input;
  };
	/**
	 * getUrl
	 */
  this.getUrl = function () {
    return this.url;
  };
	/**
	 * getPath
	 */
  this.getPath = function () {
    return this.path;
  };
	/**
	 * url相对地址与绝对地址处理
	 * 1.绝对地址：/a/b/c		:	http://name/age	->	http://a/b/c
	 * 2.附加地址：+a/b/c 等于 +/a/b/c	:	http://name/age	->	http://name/age/a/b/c
	 * 3.相对地址：a/b/c		:	http://name/age	->	http://name/a/b/c
	 * 
	 */
  this.get = function (_encodeFlag) {
    var allUrl = this.url + this.getParasStr();
    return _encodeFlag === true ? encodeURI(encodeURI(allUrl)) : allUrl;
  };
};

module.exports.newInstance = function (input){
    return new Url(input);
  };